package com.geovis.emergency.spd.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.system.service.ISystemPostService;
import com.geovis.emergency.spd.entity.system.entity.SystemPost;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemPostSaveDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemPostUpdateDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 系统管理模块-岗位接口
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/6/27
 */
@RestController
@RequestMapping("/system/systemPost")
@RequiredArgsConstructor
@Api(value = "系统管理模块-岗位接口", tags = "系统管理模块-岗位接口")
@Slf4j
@Validated
public class SystemPostController extends BaseController<ISystemPostService> {
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping({"/save"})
    public Result save(@Validated @RequestBody SystemPostSaveDTO saveDTO) {
        if (baseService.count(Wrappers.<SystemPost>lambdaQuery().eq(SystemPost::getName,saveDTO.getName())) == 0){
            baseService.save(BeanUtil.toBean(saveDTO, SystemPost.class));
            return Result.ok();
        }else {
            return Result.failed("该岗位已经存在了");
        }
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result update(@Validated @RequestBody SystemPostUpdateDTO updateDTO) {
        if (baseService.count(Wrappers.<SystemPost>lambdaQuery()
                .eq(SystemPost::getName,updateDTO.getName())
                .ne(SystemPost::getId,updateDTO.getId())) == 0){
            baseService.updateById(BeanUtil.toBean(updateDTO, SystemPost.class));
            return Result.ok();
        }else {
            return Result.failed("该岗位已经存在了");
        }
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.LIST_VALUE, notes = SwaggerConstants.LIST_NOTES)
    @PostMapping("/list")
    public Result<List<SystemPost>> list() {
        return Result.ok(baseService.list(Wrappers.lambdaQuery(SystemPost.class)));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = SwaggerConstants.GET_BY_ID_VALUE, notes = SwaggerConstants.GET_BY_ID_NOTES)
    @GetMapping("/getById/{id}")
    public Result<SystemPost> getValueById(@PathVariable("id") String id) {
        return Result.ok(baseService.getById(id));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }
}
